<script setup>
import { User, View, Unlock } from '@element-plus/icons-vue'
import { ref, reactive ,onMounted} from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { Getlogin,Getlogincode } from '@/api/login.js'
import useToken from '@/stores/token'
import _ from 'lodash'
const router = useRouter()
const formData = ref({
  account: 'admin',
  password: '1234',
  code: ''
})
const rules = reactive({
  account: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
  password: [
    {
      required: true,
      message: '密码不能为空',
      trigger: 'blur'
    }
  ],
  code: [
    {
      required: true,
      message: '验证码不能为空',
      trigger: 'blur'
    }
  ]
})
const formRef = ref()
const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate(async (valid) => {
    if (valid) {
      const { updateToken } = useToken()
      const {
        data: { token }
      } = await Getlogin(formData.value)
      updateToken(token) // 更新pinia里面的token
      ElMessage({
        showClose: true,
        message: '登录成功',
        type: 'success'
      })
      router.push('/')
    } else {
      ElMessage({
        showClose: true,
        message: '登录失败',
        type: 'error'
      })
    }
  })
}
// 验证码
const imgUrl = ref('')
const getImgcode = async () => {
  const res = await Getlogincode()
  console.log(res, 111);
  const fr = new FileReader()
  fr.onload = (event) => {
        // 表示读取完毕
        imgUrl.value = event.target.result
      }
    fr.readAsDataURL(res)
}
const changeImageCode = _.debounce(function() {
    getImgcode()
}, 1000)
onMounted(() =>{
    getImgcode()
})
</script>
<template>
  <div class="login-container">
    <div class="form">
      <img src="../../assets/hahaha.jpg" alt="" >
      <el-card class="login-card">
       <div style="display:flex;align-items:center;margin-bottom:20px;margin-left:30px"> <img id="titleimg" src="../../assets/images/login-logo.png" alt="" style="align-self:center"><h3 style="align-self:center">客达天下</h3></div>
        <el-row type="flex" justify="center" align="center">
          <el-form
            label-width="30px"
            style="max-width: 460px; min-height: 300px"
            :model="formData"
            ref="formRef"
            :rules="rules"
          >
            <el-form-item style="width: 300px" prop="account">
              <el-input placeholder="账号" :prefix-icon="User" v-model="formData.account"  size="large"/>
            </el-form-item>
            <el-form-item style="width: 300px" prop="password">
              <el-input
                placeholder="密码"
                type="password"
                :prefix-icon="View"
                v-model="formData.password"
                size="large"
              />
            </el-form-item>
            <el-form-item style="width: 300px" prop="code">
              <el-input placeholder="验证码" v-model="formData.code" :prefix-icon="Unlock"  size="large"></el-input>
              <span class="YZM"><img :src="imgUrl" @click="changeImageCode" id="imgs"/></span>
            </el-form-item>
            <el-form-item>
              <el-button style="width: 270px" type="primary" @click="submitForm(formRef)"  size="large"
                >进入</el-button
              >
            </el-form-item>
          </el-form>
        </el-row>
      </el-card>
    </div>
  </div>
</template>

<style lang="less" scoped>
.login-container {
  height: 100vh;
  background-color:rgb(43, 48, 60) ;
  background-size: cover;
  display: flex;
  align-items: center;
  // flex: 2;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding-left: 50px;

  
.form{
 display: flex;
  width: 950px;
  height: 442px;
  // background-color: rgb(236, 39, 39);
  img{
    // float: right;
  width: 530px;
  height: 442px;
  display: inline-block;
}
  .el-card {
    // float: left;
    border: none;
    padding: 20px;
    width: 400px;
    height: 400px;
    border: 1px solid rgb(201, 201, 201);
    // margin-left:8px
  }
}
}
::v-deep #titleimg{
  width: 50px;
  height: 50px;
  margin-left: 50px;
}
h3{
  margin-left:30px;
  margin-top:10px;
  display: inline-block;
}
.YZM{
  position: relative;
    left: 177px;
    top: -39px;
    width: 92px;
    height: 38px;
  #imgs{
    width: 100%;
    height: 100%;
  }
 
}
</style>
